package com.example.gymmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.User;
import com.example.gymmanagement.service.LoginService;
import com.example.gymmanagement.service.UserService;
import com.example.gymmanagement.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.desktop.QuitEvent;

/**
* @author 蜗牛菌
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-06-05 20:47:33
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService{
    //Lazy避免循环注入
    @Autowired
    private UserMapper userMapper;
    @Autowired
    @Lazy
    private LoginService loginService;

    @Override
    public boolean insert(User user) {
        //UserMapper.xml中的方法
        return this.userMapper.insert(user) >0;
    }

    @Override
    public User selectById(Integer id) {
        //Basemapper里的方法
        return (User)this.userMapper.selectById(id);
    }

    @Override
    public boolean deleteById(Integer id) {
        //BaseMapper里的方法
        return this.userMapper.deleteById(id)>0;
    }

    @Override
    public boolean updateUser(User user) {
        return this.userMapper.updateById(user)>0;
    }

    @Override
    public Result selectList(Integer index, Integer size) {
        //默认第一页开始，每页5条
        if(index==null ||index==0){
            index = 1;
        }
        if(size == null||size==0){
            size = 5;
        }
        Page<User> pageInfo = new Page(index,size);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        Page<User> userList = (Page) this.userMapper.selectPage(pageInfo,lambdaQueryWrapper);
        Integer code = userList!=null? StatusCode.GET_OK:StatusCode.GET_ERR;
        String msg = userList!=null?"查询成功":"查询失败";
        return new Result(code,userList,msg);
    }

    @Override
    public Result select(Integer index, Integer size, User user) {
        //默认第一页开始，每页5条
        if(index==null ||index==0){
            index = 1;
        }
        if(size == null||size==0){
            size = 5;
        }
        Page<User> pageInfo = new Page(index,size);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(user.getUserNum()!=null){
            queryWrapper.like("user_num",user.getUserNum());
        }if (user.getUserName()!=null&&user.getUserName()!=""){
            queryWrapper.like("user_name",user.getUserName());
        }if (user.getUserAuth()!=null&&user.getUserAuth()!=""){
            queryWrapper.like("user_auth",user.getUserAuth());
        }if (user.getUserEmail()!=null&&user.getUserEmail()!=""){
            queryWrapper.like("user_email",user.getUserEmail());
        }if (user.getUserPassword()!=null&&user.getUserPassword()!=""){
            queryWrapper.like("user_password",user.getUserPassword());
        }if (user.getUserIcon()!=null){
            queryWrapper.like("user_icon",user.getUserIcon());
        }if (user.getUserCred()!=null){
            queryWrapper.like("user_cred",user.getUserCred());
        }if (user.getUserCreatetime()!=null&&user.getUserCreatetime().toString()!=""){
            queryWrapper.like("user_createtime",user.getUserCreatetime());
        }if (user.getUserUpdatetime()!=null&&user.getUserUpdatetime().toString()!=""){
            queryWrapper.like("user_updatetime",user.getUserUpdatetime());
        }
        Page<User> userList = (Page) this.userMapper.selectPage(pageInfo,queryWrapper);
        Integer code = userList!=null? StatusCode.GET_OK:StatusCode.GET_ERR;
        String msg = userList!=null?"查询成功":"查询失败";
        return new Result(code,userList,msg);
    }

    //验证用户名和密码
    @Override
    public User findUser(long userId, String userpassword) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserNum,userId);
        queryWrapper.eq(User::getUserPassword,userpassword);
        //查到一条符合就不再往下查找了
        queryWrapper.last("limit 1");
        return (User)this.userMapper.selectOne(queryWrapper);
    }

    //按token查询当前登录用户
    @Override
    public Result selectCurrentUser(String token) {
        User user = this.loginService.checkToken(token);
        //验证token是否合法
        if (user ==null){
            return new Result(StatusCode.GET_ERR,"token不合法");
        }
        //验证失败，返回错误信息
        //验证成功，返回对应结果
        return new Result(StatusCode.GET_OK,user,"获取成功");
    }


}




